﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;

namespace DbView
{
	/*
		Class to wrap use of the extended properties on rows, columns and 
		the table itself
	*/
	public class DataTableExtraInfoManager
	{
		public DataTableExtraInfoManager(DataTable targetTable)
		{
			m_DataTable = targetTable;
		}
	
		public DataTable Table { get { return m_DataTable; } }
		// Pkey columns
		public void SetPkey(DataColumn dc) { SetColumnProperty(dc, "PKEY", "Y"); }
		public bool IsPkey(DataColumn dc)
		{ 
			String pkeyValue = GetColumnProperty(dc, "PKEY", "N");
			return (pkeyValue == "Y");
		}

		public void SetFkey(DataColumn dc) { SetColumnProperty(dc, "FKEY", "Y"); }
		public bool IsFkey(DataColumn dc)
		{
			String keyValue = GetColumnProperty(dc, "FKEY", "N");
			return (keyValue == "Y");
		}

		public void SetIndexed(DataColumn dc) { SetColumnProperty(dc, "IDX", "Y"); }
		public bool IsIndexed(DataColumn dc)
		{
			String keyValue = GetColumnProperty(dc, "IDX", "N");
			return (keyValue == "Y");
		}

		// flag a column as a parameter for a command.
		// prevents xml command strings having to persist the whole row's data		
		public void SetParam(DataColumn dc, String name) { SetColumnProperty(dc, "PARAM", name); }
		public String Param(DataColumn dc)
		{
			String paramValue = GetColumnProperty(dc, "PARAM", "");
			return paramValue;
		}

		// hold sort via column
		public void SetSorted(DataColumn dc, bool descending)
		{
			String ad = (descending) ? "D" : "A";
			SetColumnProperty(dc, "SORT", ad);
		}
		// 1 = asc -1 = desc 0 = not sorted
		public int IsSorted(DataColumn dc)
		{
			String keyValue = GetColumnProperty(dc, "SORT", "");
			return (keyValue == "D") ? -1 :
					(keyValue == "A") ? 1 : 0;
		}
		
		public String Command 
		{
			get { return GetExtraInfo("CMD", ""); }
			set { SetExtraInfo("CMD", value); }
		}
	
		public void SetModifiable() { SetExtraInfo("MOD", "Y"); }
		public bool IsModifiable()
		{
			String keyValue = GetExtraInfo("MOD", "N");
			return (keyValue == "Y");
		}
		
		// set this to override the display in the status bar.
		public String GridDescription
		{
			get { return GetExtraInfo("RPTDESC", ""); }
			set { SetExtraInfo("RPTDESC", value); }
		}

		// suggest we stick to strings for props for now
		public void SetColumnProperty(DataColumn column, String property, Object value)
		{
			try 
			{
				if (column.ExtendedProperties.ContainsKey(property))
					column.ExtendedProperties[property] = value;
				else
					column.ExtendedProperties.Add(property, value);
			}
			catch (Exception e)
			{
				Console.WriteLine(e.Message);
			}
		}
		
		public String GetColumnProperty(DataColumn column, String property, String defaultValue)
		{
			if (column.ExtendedProperties.ContainsKey(property))
				return column.ExtendedProperties[property].ToString();
			else
				return defaultValue;
		}

		public void SetExtraInfo(String property, Object value)
		{
			if (m_DataTable.ExtendedProperties.ContainsKey(property))
				m_DataTable.ExtendedProperties[property] = value;
			else
				m_DataTable.ExtendedProperties.Add(property, value);
		}

		public String GetExtraInfo(String property, String defaultValue)
		{
			if (m_DataTable.ExtendedProperties.ContainsKey(property))
				return m_DataTable.ExtendedProperties[property].ToString();
			else
				return defaultValue;
		}
		
		// Row colouring
		// The Colour parameter is one of the inbuilt colours eg Red, Bisque etc.
		// 
		public void SetRowColour(String colour, int[] rows)
		{
			String colourProp = "COLOURS";
			String value = this.GetExtraInfo(colourProp, "");
			if (value.Length > 0) value += ",";
			value += colour;
			SetExtraInfo(colourProp, value);
			StringBuilder sb = new StringBuilder();
			bool first = true;
			foreach (int row in rows)
			{
				if (first == true)
					first = false;
				else
					sb.Append(",");
				sb.Append(row);
			}
			SetExtraInfo(colour, sb.ToString());
		}

		public String[] GetRowColours()
		{
			String colourProp = "COLOURS";
			String value = GetExtraInfo(colourProp, "");
			if (value.Length > 0)
				return value.Split(',');
			else
				return new String[]{};
		}

		public int[] GetColourRows(String colour)
		{
			String rowString = GetExtraInfo(colour, "");
			if (rowString.Length > 0)
			{
				String[] srows = rowString.Split(',');
				int [] rows = new int[srows.GetLength(0)];
				for (int idx = 0; idx < srows.GetLength(0); ++idx)
				{
					rows[idx] = Convert.ToInt32(srows[idx]);
				}
				return rows;
			}
			else
				return new int[]{};
		}

		DataTable m_DataTable;
	}
}
